package com.bgybmi.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bgybmi.config.ConfigEnum;
import com.bgybmi.controller.repair.BxThread;
import com.bgybmi.entity.BxCreate;
import com.bgybmi.entity.BxImgCreate;
import com.bgybmi.entity.JbPhone;
import com.bgybmi.sender.Sender;
import com.bgybmi.sender.WeChatFactory;
import com.jfinal.kit.PropKit;

public class BxService {
	
	public static final BxService me=new BxService();

	/**
	 * ##insert
	 */
	
	public boolean bxCreateSubmit(String loginuser, String bxr, String phone, String bxposition, String bxdetail,
			Boolean urgent, String serverIds,String url) {
		// TODO Auto-generated method stub
		Boolean saveflag=false;
		//报修信息的存入
		BxCreate bc=new BxCreate();
		bc.set("cjr",loginuser);
		bc.set("bxr",bxr);
		bc.set("phone",phone);
		bc.set("bxposition",bxposition);
		bc.set("bxdetail",bxdetail);
		bc.set("urgent",urgent);
		bc.set("dt_create", new Date());
		try {
			bc.save();
			saveflag=true;
			url=url+"&id="+bc.get("id");
			//+ "meetingorder?roomname="+room;//这里的路径据说他们不是 别
			String msg="报修信息已记录,"+bxdetail+",<a href=\""+url+"\">查看</a>";
			System.out.println(url);
			//sendTextMssage(chr.replace(",", "|"),msg);
			WeChatFactory wcf=new WeChatFactory(loginuser,msg,PropKit.get(ConfigEnum.BX.getAgentId()));
			Sender pro= wcf.produce();
			pro.Send();
/*			SendMsg sm=new SendMsg(bxr.replace(",", "|"),msg);
			sm.start();*/
			//线程
			BxThread bx=new BxThread(serverIds,bc.getInt("id"));
			bx.start();
		} catch (Exception e) {
			 //TODO: handle exception
			throw new RuntimeException(e);
		}
		return saveflag;		
	}	
	
	/**
	 * ##update
	 */
	public boolean BxXqConfirm(int id,String loginuser,String url){
		Boolean saveflag=false;
		BxCreate bc = new BxCreate().findById(id);
		bc.set("status","B");
		try {			
			bc.update();
			saveflag=true;
			String msg="报修已确认";
			WeChatFactory wcf=new WeChatFactory(loginuser,msg,PropKit.get(ConfigEnum.BX.getAgentId()));
			Sender pro= wcf.produce();
			pro.Send();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return saveflag;			
	}
	public boolean BxXqCancel(int id,String loginuser,String url){
		Boolean saveflag=false;
		BxCreate bc = new BxCreate().findById(id);
		bc.set("status","C");
		try {
			bc.update();
			saveflag=true;
			url=url+"&id="+bc.get("id");
			String msg="报修已取消";
			WeChatFactory wcf=new WeChatFactory(loginuser,msg,PropKit.get(ConfigEnum.BX.getAgentId()));
			Sender pro= wcf.produce();
			pro.Send();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return saveflag;
	}
	
	/**
	 * query
	 * @param user
	 * @return
	 */
	public List<BxCreate> listAllByUser(String user) {
		// TODO Auto-generated method stub
		return BxCreate.me.listAllByUser(user);
	}

	public List<JbPhone> listAllPhone() {
		// TODO Auto-generated method stub
		return JbPhone.me.listAllPhone();
	}

	public List<BxCreate> listAllByParamAndUser(String status, String user) {
		// TODO Auto-generated method stub
		return BxCreate.me.listAllByParamAndUser("status='"+status+"'",user);
	}
	
	
	
	public List<Map> bxmsg(String user){		
		//find By user
		List<BxCreate> bxlis=BxCreate.me.listAllByParamAndUser("status <> 'E'",user);//拿到Bx列表
		List<Map> results = new ArrayList<Map>();
		for (BxCreate bc : bxlis) {
			Map every=new HashMap();
			every.put("id",bc.getInt("id"));
			every.put("bxr", bc.getStr("bxr"));
			every.put("phone",bc.getInt("phone"));
			every.put("bxposition",bc.getStr("bxposition"));
			every.put("bxdetial",bc.getStr("bxdetial"));
			every.put("urgent", bc.getBoolean("urgent"));
			List<BxImgCreate> bicli=BxImgCreate.me.listFindById(bc.getInt("id"));
			every.put("imgli",bicli);
			results.add(every);
		}
		return results;
		//添入图片列表，数据结构的存入,-->遍历访问数据库压力呢?
	}

	public Map bxXqRecord(int id) {
		// TODO Auto-generated method stub
		BxCreate bc = BxCreate.me.findById(id);
		List<BxImgCreate> bicli=BxImgCreate.me.listFindById(id);
		Map results=new HashMap();
		results.put("res",id);
		results.put("bx",bc);
		results.put("imgli",bicli);
		return results;
	}
	
	
}
